<?php

namespace App\Http\Controllers\EmberApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use GuzzleHttp\Client;

class EmberApiController extends Controller
{
    protected $apiUrl = '';

    public function __construct()
    {
        $url = env('OPENXDAGPOOL_API_URL');
        if (!$url || (strpos($url, 'https://') !== 0 && strpos($url, 'http://') !== 0))
            throw new \InvalidArgumentException('.env setting OPENXDAGPOOL_SCRIPTS_URL invalid.');
        $this->apiUrl = $url;
        //var_dump($this->apiUrl);
    }

    public  function getData($url){
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//设置url属性
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);//获取数据
        curl_close($ch);//关闭curl
        return $output;

    }

    //获取后端stats接口数据
    public function stats(){
        $cache = Redis::get('xdagpoolstats');
        $stats = json_encode([]);
        if (!isset($cache)){
            $url = $this->apiUrl.'stats';
            $data = $this->getData($url);
            Redis::setex('xdagpoolstats',60,$data);
            $stats = json_decode($data);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);
    }

    //获取后端accounts接口数据
    public  function accounts(Request $request){
        //var_dump($address);
        //通过矿工地址查询到miners表中对应的记录
        //$address = urldecode($address);
        $requestUri = $request->getRequestUri();
        $requestPath = '/'.$request->path().'/?address=';
        $address = substr($requestUri,strlen($requestPath));
        //var_dump($address);
        //
        $cache = Redis::get($address);
        $stats = json_encode([]);
        if(!isset($cache)){
            $url = $this->apiUrl.'accounts/?address='.$address;
            $stats = $this->getData($url);
            Redis::setex($address,60,$stats);
            $stats = json_decode($stats);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);
    }

    //获取后端blocks接口数据
    public  function blocks(){
        $cache = Redis::get('xdagpoolblocks');
        $stats = json_encode([]);
        if(!isset($cache)){
            $url = $this->apiUrl.'blocks';
            $stats = $this->getData($url);
            Redis::setex('xdagpoolblocks',60,$stats);
            $stats = json_decode($stats);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);
    }

    //获取后端miners接口数据
    public  function miners(){
        $cache = Redis::get('xdagpoolminers');
        $stats = json_encode([]);
        if(!isset($cache)){
            $url = $this->apiUrl.'miners';
            $stats = $this->getData($url);
            Redis::setex('xdagpoolminers',60,$stats);
            $stats = json_decode($stats);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);
    }

    //获取后端payments接口数据
    public function payments(){
        $cache = Redis::get('xdagpoolpayments');
        $stats = json_encode([]);
        if(!isset($cache)){
            $url = $this->apiUrl.'payments';
            $stats = $this->getData($url);
            Redis::setex('xdagpoolpayments',60,$stats);
            $stats = json_decode($stats);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);
    }
}
